import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_dva/flutter_dva.dart';
import 'package:todo_app/layout/Header.dart';
import 'package:todo_app/models/system.dart';
import 'package:todo_app/utils/mqtt/LocalMqttClient.dart';
import 'package:todo_app/utils/SharedPreferencesUtil.dart';
import 'package:todo_app/utils/mqtt/MessageDBUtils.dart';
import 'package:todo_app/utils/mqtt/UserDBUtils.dart';
import 'package:wakelock/wakelock.dart';

import 'app.dart';

Dva dva = Dva(DvaOpts(initialState: <String, StoreOfState<dynamic>>{
  'system': StoreOfState<SystemState>(state: SystemState()),
}, models: <Model<dynamic>>[
  SystemModel(),
]));

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Wakelock.enable();
  SystemChrome.setSystemUIOverlayStyle(lightBrightness);
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
  EasyRefresh.defaultHeaderBuilder = () => const ClassicHeader(
    dragText: '下拉刷新',
    armedText: '释放刷新',
    readyText: '刷新中',
    processingText: '刷新中',
    processedText: '刷新成功',
    noMoreText: '没有更多数据',
    failedText: '刷新失败',
    messageText: '更新于 %T',
  );
  EasyRefresh.defaultFooterBuilder = () => const ClassicFooter(
    dragText: '上拉加载',
    armedText: '释放加载',
    readyText: '加载中',
    processingText: '加载中',
    processedText: '加载成功',
    noMoreText: '没有更多数据',
    failedText: '加载失败',
    messageText: '更新于 %T',
  );

  WidgetCreatorFunction app = await dva.start(() => const MyApp(), () async {
    final ReduxPersistor persistor =
        ReduxPersistor(store: dva.store!, heartBeat: 450);
    await SharedPreferenceUtil.getInstance();
    await persistor.persist();
    await MessageDBUtils.init();
    await UserDBUtils.init();
    await LocalMqttClient.connect();
    // Provider().store!.rootState.system!.loginSuccess = false;
  }, waitCallabck: true);
  return runApp(app());
}
